package kr.pfg.applications.captureyourscreen.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.Screen;
	import flash.display.Shape;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	import kr.as3.display.NewWindow;

	public class FullScreenImageWindow extends NewWindow
	{
		private var captureBitmapData:BitmapData;
		private var captureBitmap:Bitmap;
		
		private var bgBitmapData:BitmapData;
		private var bgBitmap:Bitmap;
		
		private var frame:FrameWindow;
		private var frameMask:Shape;
		
		public function FullScreenImageWindow( bmd:BitmapData )
		{
			super( NativeWindowType.NORMAL, NativeWindowSystemChrome.NONE, false );
			
			this.bgBitmapData = bmd.clone();
			this.bgBitmapData.colorTransform( this.bgBitmapData.rect, new ColorTransform( 0.8, 0.8, 0.8 ) );
			this.bgBitmap = new Bitmap( this.bgBitmapData );
			this.stage.addChild( this.bgBitmap );
			
			this.captureBitmapData = bmd;
			this.captureBitmap = new Bitmap( this.captureBitmapData );
			this.stage.addChild( this.captureBitmap );
			
			this.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
			
			var screenBound:Rectangle = Screen.mainScreen.bounds;
			this.width = screenBound.width;
			this.height = screenBound.height;
			
			this.activate();
			
			var blurFilter:BlurFilter = new BlurFilter( 4, 4, 2 );
			this.bgBitmap.filters = [ blurFilter ];
			
			this.frame = new FrameWindow();
			this.frame.x = screenBound.width / 2 - this.frame.width / 2;
			this.frame.y = screenBound.height / 2 - this.frame.height / 2;
			this.frame.dragArea.addEventListener( MouseEvent.MOUSE_DOWN, this.startFrameDrag );
			this.frame.clickArea.addEventListener( MouseEvent.MOUSE_DOWN, this.clickAreaDownHandler );
			this.stage.addChild( this.frame );
			
			this.frameMask = new Shape();
			this.frameMask.graphics.beginFill( 0x000000 );
			this.frameMask.graphics.drawRect( 0, 0, this.frame.clickArea.width, this.frame.clickArea.height );
			this.frameMask.x = this.frame.x + 10;
			this.frameMask.y = this.frame.y + 20;
			this.stage.addChild( this.frameMask );
			this.captureBitmap.mask = this.frameMask;
			
			
		}
		
		/**
		 * Frame drag.
		 */ 
		
		private function startFrameDrag( e:MouseEvent ):void
		{
			this.frame.startDrag();
			this.stage.addEventListener( Event.ENTER_FRAME, this.maskFollowFrame );
			this.stage.addEventListener( MouseEvent.MOUSE_UP, this.stopFrameDrag );
			this.stage.addEventListener( Event.MOUSE_LEAVE, this.stopFrameDrag );
		}
		
		/**
		 * Frame stop.
		 */ 
		
		private function stopFrameDrag( e:Event ):void
		{
			this.frame.stopDrag();
			this.stage.removeEventListener( Event.ENTER_FRAME, this.maskFollowFrame );
			this.stage.removeEventListener( MouseEvent.MOUSE_UP, this.stopFrameDrag );
			this.stage.removeEventListener( Event.MOUSE_LEAVE, this.stopFrameDrag );
		}
		
		/**
		 * Mask always follow frame's move.
		 */ 
		
		private function maskFollowFrame( e:Event ):void
		{
			this.frameMask.x = this.frame.x + 10;
			this.frameMask.y = this.frame.y + 20;
		}
		
		/**
		 * When user click a clickArea, copy a bitmapData from inside of frame.
		 */ 
		
		private function clickAreaDownHandler( e:MouseEvent ):void
		{
			this.stage.displayState = StageDisplayState.NORMAL;
			this.stage.removeChild( this.bgBitmap );
			this.stage.removeChild( this.captureBitmap );
			this.stage.removeChild( this.frameMask );
			this.frame.dragArea.removeEventListener( MouseEvent.MOUSE_DOWN, this.startFrameDrag );
			this.frame.clickArea.removeEventListener( MouseEvent.MOUSE_DOWN, this.clickAreaDownHandler );
			
			var captureBmd:BitmapData = new BitmapData( this.frame.clickArea.width, this.frame.clickArea.height );
			var matrix:Matrix = new Matrix();
			matrix.translate( -this.frame.x - 10, -this.frame.y - 20 );
			captureBmd.draw( this.captureBitmapData, matrix );
			this.frame.setCaptureImage( captureBmd );
			this.frame.addEventListener( NativeDragEvent.NATIVE_DRAG_COMPLETE, this.frameClose );
			
			this.x = this.frame.getBounds( this.stage ).x;
			this.y = this.frame.getBounds( this.stage ).y;
			this.width = this.frame.width;
			this.height = this.frame.height;
			this.frame.x = 0;
			this.frame.y = 0;
		}
		
		/**
		 * When user drag, close a frame.
		 */ 
		
		private function frameClose( e:NativeDragEvent ):void
		{
			this.stage.removeChild( this.frame );
			this.close();
		}
	}
}